package com.iraeta.controller.basedata;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.controller.BaseController;
import com.iraeta.entity.Scviewrecord;
import com.iraeta.entity.Scviewrecordmx;
import com.iraeta.entity.vo.ScviewrecordVo;
import com.iraeta.service.basedata.ScviewrecordService;
import com.iraeta.service.sys.ISysFilesService;
import com.iraeta.service.sys.ISysOrguserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController(value = "ScviewrecordController")
@RequestMapping("scviewrecord")
public class ScviewrecordController extends BaseController {
    @Autowired
    ISysOrguserService iSysOrguserService;
    @Autowired
    ScviewrecordService scviewrecordService;
    @Autowired
    ISysFilesService iSysFilesService;

    @RequestMapping("pagescviewrecord")
    public String pageScViewRecord(@RequestBody JSONObject jo){
        Integer orgId = iSysOrguserService.selectByUserId(this.getUserId()).getOrgId();
        this.pageSet(jo);
        List<ScviewrecordVo> ScviewrecordList = scviewrecordService.selectScviewrecordByCondition(orgId == null ? 0 : orgId);
        PageInfo<ScviewrecordVo> scviewrecordPageInfo = this.getPageInfo(ScviewrecordList);
        return this.success(scviewrecordPageInfo);
    }

    @RequestMapping("getscviewrecord")
    public String getScViewRecord(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        Scviewrecord scviewrecord = scviewrecordService.selectScviewrecordById(id);
        return this.success(scviewrecord);
    }

    @RequestMapping("addbyupload")
    public String addByUpload(@RequestBody JSONObject paramJb){
        int fileId = paramJb.getIntValue("fileId");
        Integer orgId = iSysOrguserService.selectByUserId(this.getUserId()).getOrgId();
        Scviewrecord scviewrecord = new Scviewrecord();
        scviewrecord.setFileid(fileId);
        scviewrecord.setUserid(this.getUserId());
        scviewrecord.setOrgid(orgId);
        scviewrecord.setCreatetime(new Date());
        List<Scviewrecordmx> scviewrecordmxList = new ArrayList<>();
        scviewrecordService.setScviewrecord(scviewrecord, scviewrecordmxList);
        scviewrecordService.addScviewrecord(scviewrecord, scviewrecordmxList);
        return this.success();
    }

    /*废弃*/
    @RequestMapping("addeditscviewrecord")
    public String addScViewRecord(@RequestBody JSONObject paramJb){
        int recordId = paramJb.getIntValue("id");
        Scviewrecord scviewrecord = recordId <= 0 ? new Scviewrecord() :  scviewrecordService.selectScviewrecordById(recordId);
        this.mapJsonToObj(paramJb, scviewrecord);
        if(recordId <= 0) scviewrecord.setId(0);
        if(recordId <= 0){
            scviewrecordService.addScviewrecord(scviewrecord, new ArrayList<Scviewrecordmx>());
        }else {
            scviewrecordService.updateScviewrecord(scviewrecord);
        }
        return this.success();
    }

    @RequestMapping("deletescviewrecord")
    public String deleteScViewRecord(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        Scviewrecord scviewrecord = scviewrecordService.selectScviewrecordById(id);
        iSysFilesService.deleteSysFilesByFId((long)scviewrecord.getFileid());
        scviewrecordService.deleteScviewrecord(id);
        return this.success();
    }
}
